$(function () {

    Table.builder()
        .listUrl(COMPANY_CALIBRATION_LIST)
        .createUrl(COMPANY_CALIBRATION_CREATE)
        .updateUrl(COMPANY_CALIBRATION_UPDATE)
        .detailUrl(COMPANY_CALIBRATION_DETAIL)
        .deleteUrl(COMPANY_CALIBRATION_DETELE)
        .customValidate(checkCommon)
        .onLoadSuccess(mergeCells)
        .init();

    SelectCascade.builder()
        .dom($("#baitCompanyId"))
        .url(SELECT_COMPANY_CADE)
        .initByUrl();

    function checkCommon(validatorDom) {
        if (validatorDom.attr("name") === "shortName" && validatorDom.val().length > 100) {
            return {
                "retBool": true,
                "msg": "标定别名长度不能超过100个字符"
            };
        }
        if (validatorDom.attr("name") === "normalSpeed" && validatorDom.val() < 0) {
            return {
                "retBool": true,
                "msg": "低速转速不能小于0"
            };
        }
        if (validatorDom.attr("name") === "normalSecs" && validatorDom.val() < 0) {
            return {
                "retBool": true,
                "msg": "低速标定不能小于0"
            };
        }
        if (validatorDom.attr("name") === "highSpeed" && validatorDom.val() < 0) {
            return {
                "retBool": true,
                "msg": "高速转速不能小于0"
            };
        }
        if (validatorDom.attr("name") === "highSecs" && validatorDom.val() < 0) {
            return {
                "retBool": true,
                "msg": "高速标定不能小于0"
            };
        }
    }

    $(".creates").on("click", function () {
        document.getElementById("baitBrandId").innerHTML = "";
        document.getElementById("baitSizeId").innerHTML = "";
    })

    $("#tableAjaxId").on("click", ".jiaozhun", function () {
        var id = $.trim($(this).attr("dataId"));
        window.location.href = "/admin/system/calibration/calibration.html?id=" + id
    })
});


columns = [
    {
        field: 'baitCompanyName',
        title: '标定名称',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            return value + '-' + row.baitBrandName + '-' + row.baitSizeName + "mm";
        }
    }, {
        field: 'normalValue',
        title: '校准结果',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            var calibrationValue = "(";
            if (value !== "") {
                calibrationValue += value + ";";
            }
            if (row.highValue !== "") {
                calibrationValue += row.highValue + ";";
            }
            calibrationValue += ")";
            return calibrationValue;
        }
    }, {
        field: 'shortName',
        title: '标定别名',
        align: 'center',
        valign: 'middle'
    }, {
        field: 'normalSpeed',
        title: '低速模式(r/min)',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            return PriceUtil.getPrice(value, 100);
        }
    }, {
        field: 'normalSecs',
        title: '低速时间(min)',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            return (PriceUtil.getPrice(value, 100) / 60).toFixed(2);
        }
    }, {
        field: 'highSpeed',
        title: '高速模式(r/min)',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            return PriceUtil.getPrice(value, 100);
        }
    }, {
        field: 'highSecs',
        title: '高速时间(min)',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            return (PriceUtil.getPrice(value, 100) / 60).toFixed(2);
        }
    }, {
        field: 'lastModifyUser',
        title: '最后修改人',
        align: 'center',
        valign: 'middle'
    }, {
        field: 'lastModifyTime',
        title: '最后修改时间',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            return DateUtil.changeDateToString(value, DateFormatEnum.YYYY_MM_DD);
        }
    }, {
        title: "操作",
        align: 'center',
        valign: 'middle',
        width: 230,
        formatter: option
    }
]

function option(value, row, index) {
    var html = '<div class="d-flex justify-content-center">';
    html += ' <a class="btn btn-xs btn-default m-r-5 edits" data-toggle="modal" data-target="#commonDiv" dataId="' + row.id + '" title="编辑" >编辑</a>'
        + '<a class="btn btn-xs btn-default m-r-5 deletes" data-toggle="modal" data-target=".removeDio" dataId="' + row.id + '"  title="删除" >删除</a>'
        + '<a class="btn btn-xs btn-default m-r-5 jiaozhun" data-toggle="modal" data-target="#jiaozhunDiv" dataId="' + row.id + '" title="校准" >校准</a>'
        + '<a class="btn btn-xs btn-default"  target="_blank" href="/admin/system/calibration/calibration_record.html?id=' + row.id + '" title="校准记录" >校准记录</a>';
    html += '</div>'
    return html;
}
function mergeCells() {
    var table = document.getElementById("tableAjaxId");
    var rows = table.rows;
    var currentCompany = null;
    var rowspan = 1;

    for (var i = 1; i < rows.length; i++) {
        var company = rows[i].cells[0].innerText;
        if (company == currentCompany) {
            rowspan++;
            rows[i].deleteCell(0);
            rows[i - rowspan + 1].cells[0].rowSpan = rowspan;
        } else {
            currentCompany = company;
            rowspan = 1;
        }
    }
}